﻿using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Data.Entity.Design.Extensibility;
using System.ComponentModel;

namespace Liekhus.EntityFramework.XAFExtensions.CustomAttributes
{
    /// <summary>
    /// Custom Entity Designer Property extension used to create a Size attribute
    /// on XAF/XPO Business Object properties
    /// </summary>
    public class SizeProperty : BaseCustomAttribute<int>
    {
        #region Constructor
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="context"></param>
        public SizeProperty(XElement parent, PropertyExtensionContext context) : base(parent, context)
        {
            XAttribute xaType;

            //Load up the valid Property Types that this Custom Property can be applied against
            this.ValidPropertyTypeNames.Add("String");
            this.ValidPropertyTypeNames.Add("Guid");
            this.ValidPropertyTypeNames.Add("Xml");

            //Extract the Type value for for this XElement
            xaType = parent.Attributes(XName.Get("Type")).FirstOrDefault();
            if (xaType != null)
            {
                //If the Type on this XElement is anything other than a string then this
                //custom property doesn't make any sense so clean up any existing metadata
                //in the Entity Model if it exists and set the flag indicating that this property
                //should not be displayed in the EDM Properties window.
                if (this.ValidPropertyTypeNames.Contains(xaType.Value) == false)
                {
                    base.RemovedExistingCustomProperty();
                    this.DisplayPropertyInEDM = false;
                }
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// 
        /// </summary>
        public override string PropertyName 
        { 
            get { return "Size"; } 
        }
        /// <summary>
        /// 
        /// </summary>
        public override string PropertyFormat 
        { 
            get {
                if (propertyValue == -1)
                {
                    return "[Size(SizeAttribute.Unlimited)]";
                } else
                {
                    return "[Size({value})]"; 
                }
            } 
        }
        /// <summary>
        /// 
        /// </summary>
        public override int DefaultValue 
        { 
            get { return 50; } 
        }
        /// <summary>
        /// 
        /// </summary>
        [DisplayName("Size")]
        [Description("Size constraint for the field.")]
        public override int PropertyValue
        {
            get
            {
                return base.PropertyValue;
            }
            set
            {
                base.PropertyValue = value;
            }
        }
        #endregion
    }
}
